package cn.qy.java.base.ch21;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 打断 join
 *
 * @author acc8226
 */
public class Ch21_06_ThreadSleep {
	// 创建一个独占锁
	static Lock lock = new ReentrantLock();
	
	static class T1 extends Thread {
		public void run() {
			lock.lock();
			try {
				System.out.println("lock1 start");
				// 让出指定时间的执行权
				Thread.sleep(3000);
				System.out.println("lock1 end");
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		};
	}
	
	static class T2 extends Thread {
		public void run() {
			lock.lock();
			try {
				System.out.println("lock2 start");
				// 让出指定时间的执行权
				Thread.sleep(5000);
				System.out.println("lock2 end");
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		};
	}
	
	public static void main(String[] args) {
		new T1().start();
		new T2().start();
	}

}
